﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : UserGroup.cs
// Author            : Daniel Gray
// Created           : 01/05/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kale.Core.Security;

namespace Kale.Db.Sql
{
   public partial class UserGroup : IUserGroup
   {
      private ISecurityProvider _provider;
      private bool _loadedExplicitRoles = false;
      private bool _loadedEffectiveRoles = false;
      private bool _loadedExplicitGroups = false;
      private bool _loadedEffectiveGroups = false;

      private List<IUserRole> _explicitRoles = new List<IUserRole>();
      private List<IUserRole> _effectiveRoles = new List<IUserRole>();
      private List<IUserGroup> _explicitGroups = new List<IUserGroup>();
      private List<IUserGroup> _effectiveGroups = new List<IUserGroup>();

      internal ISecurityProvider Provider
      {
         get { return _provider; }
         set { _provider = value; }
      }

      #region ISecurityPrincipal Members

      Guid ISecurityPrincipal.Uid
      {
         get { return this.Uid; }
      }

      string ISecurityPrincipal.Name
      {
         get { return this.Name; }
      }

      #endregion

      #region ISecurityMember Members

      IUserRole[] ISecurityMember.ExplicitRoles
      {
         get
         {
            if (!_loadedExplicitRoles)
            {
               lock (_explicitRoles)
               {
                  _explicitRoles.Clear();
                  foreach (IUserRole role in _provider.GetUserRolesForUserGroup(Uid))
                  {
                     _explicitRoles.Add(role);
                  }

                  _loadedExplicitRoles = true;
               }
            }

            return _explicitRoles.ToArray();
         }
      }

      IUserRole[] ISecurityMember.EffectiveRoles
      {
         get
         {
            if (!_loadedEffectiveRoles)
            {
               lock (_effectiveRoles)
               {
                  _effectiveRoles.Clear();

                  SecurityLoader.LoadEffectiveRoles(this as ISecurityMember, _effectiveRoles);

                  _loadedEffectiveRoles = true;
               }
            }

            return _effectiveRoles.ToArray();
         }
      }

      IUserGroup[] ISecurityMember.ExplicitUserGroups
      {
         get
         {
            if (!_loadedExplicitGroups)
            {
               lock (_explicitGroups)
               {
                  _explicitGroups.Clear();

                  foreach (IUserGroup group in _provider.GetUserGroupsForUserGroup(Uid))
                  {
                     _explicitGroups.Add(group);
                  }

                  _loadedExplicitGroups = true;
               }
            }

            return _explicitGroups.ToArray();
         }
      }

      IUserGroup[] ISecurityMember.EffectiveUserGroups
      {
         get
         {
            if (!_loadedEffectiveGroups)
            {
               lock (_effectiveGroups)
               {
                  _effectiveGroups.Clear();

                  SecurityLoader.LoadEffectiveGroups(this as ISecurityMember, _effectiveGroups);

                  _loadedEffectiveGroups = true;
               }
            }

            return _explicitGroups.ToArray();
         }
      }

      #endregion
   }
}
